package com.cspg.ssss.service;


import com.cspg.ssss.domain.*;
import com.cspg.ssss.repository.*;
import com.cspg.ssss.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DwdggbmjdDataService {
    @Autowired
    private DwdggbmjdDataMapper dwdggbmjdDataMapper;
    @Autowired
    DaiBanMapper daiBanMapper;
    @Autowired
    SafeDeptMapMapper safeDeptMapMapper;
    @Autowired
    SecurityRiskMapper riskMapper;
    @Autowired
    DepartmentUserRepository departmentUserRepository;

    public int Add(DwdggbmjdTemporaryEntity dwdggbmjdDataEntity) {
        return dwdggbmjdDataMapper.insert(dwdggbmjdDataEntity);
    }

    public List<DwdggbmjdDataEntity> selectDataById(String id) {
        return dwdggbmjdDataMapper.selectDataById(id);
    }

    public List<DwdggbmjdDataEntity> selectAll(String id) {
        return dwdggbmjdDataMapper.selectAll(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public int batchInsert(List<DwdggbmjdDataEntity> list) {
        //将数据加入待办
        Map<String, Integer> map = new HashMap<>();
        for (DwdggbmjdDataEntity entity : list) {
            BacklogEntity backlog = new BacklogEntity();
            backlog.setTerm("0");
            backlog.setDbstatus("0");
            backlog.setType("1");
            backlog.setSscm("1");
            backlog.setCreattime(new Date());
            backlog.setTitle(entity.getWtms());
            backlog.setWtfxr(entity.getWtfxr());
            backlog.setWtfxsj(DateUtil.getCurrentDateStr());
            backlog.setProblem_id(entity.getId());
            if (entity.getSscm().equals("公司层面")) {
                backlog.setCheck_type("0");
                backlog.setBmmc("超高压输电公司/公司本部/安全监管部（应急指挥中心）");
            } else {
                backlog.setCheck_type("1");
                backlog.setBmmc(getSafeDeptPath(ConvertDeptToPath(entity.getBmmc())));
            }
            daiBanMapper.insertBacklog(backlog);
            if (entity.getWtyzcd().contains("违章") ||
                    entity.getWtyzcd().contains("杜绝") ||
                    entity.getWtyzcd().contains("严禁")) {
                Integer num = map.get(entity.getDwmc());
                if (num != null) {
                    num++;
                } else {
                    num = 1;
                }
                map.put(entity.getDwmc(), num);
            }
        }
        UserEntity user = departmentUserRepository.findUserInfoByUserId(list.get(0).getDrman());
        for (String key : map.keySet()) {
            Integer num = map.get(key);
            if (num != null && num != 0) {
                String id = riskMapper.selectIdByDwmc(key,null);
                if (id != null) {
                    SecurityRiskLogEntity logEntity = new SecurityRiskLogEntity();
                    logEntity.setRiskId(id);
                    logEntity.setType(3);
                    logEntity.setWarringReason("新增" + num + "条违章类、杜绝类、严禁类问题");
                    logEntity.setCreateTime(new Date());
                    logEntity.setCreateBy(user.getUser_name());
                    riskMapper.insertLog(logEntity);
                }
            }
        }
        return dwdggbmjdDataMapper.batchInsert(list);
    }

    private String getSafeDeptPath(String userdept) {
        String CompanyPath = getUserCompany(userdept);
        if (!CompanyPath.equals("")) {
            List<SafeDeptMap> ls = safeDeptMapMapper.selectDeptNameForCompany(CompanyPath);
            if (ls != null && ls.size() == 1) {
                return "超高压输电公司/" + ls.get(0).getCompany() + "/" + ls.get(0).getSafe_dept();
            }
        }

        return "";
    }

    private String getUserCompany(String userdept) {
        String[] arrPath = userdept.split("/");
        if (arrPath.length < 2) {
            return "";
        }
        if (arrPath.length >= 3 && arrPath[2].equals("海口分局")) {
            return arrPath[1] + "/" + arrPath[2];
        }
        return arrPath[1];
    }

    /**
     * 将部门路径由->分隔改成/分隔，加上超高压前缀
     */
    private String ConvertDeptToPath(String deptPath) {
        String newpath = deptPath.replaceAll("->", "/");
        newpath = "超高压输电公司/" + newpath;
        return newpath;
    }

    public int updateData(DwdggbmjdDataEntity record) {
        return dwdggbmjdDataMapper.updateData(record);
    }

    public int delete(String id) {
        return dwdggbmjdDataMapper.deleteData(id);
    }

    public DwdggbmjdDataEntity selectDataId(String id) {
        return dwdggbmjdDataMapper.selectDataId(id);
    }

    public int deleteByList(List<DwdggbmjdTemporaryEntity> list) {
        return dwdggbmjdDataMapper.deleteByList(list);
    }

    public int selectDataCount() {
        return dwdggbmjdDataMapper.selectDataCount();
    }

    public DwdggbmjdDataEntity seclectdis(String departmentName, String dwmc, String wtms, String wtfxfs, String wtfxsj) {
        return dwdggbmjdDataMapper.seclectdis(departmentName, dwmc, wtms, wtfxfs, wtfxsj);
    }

    public int insert(DwdggbmjdDataEntity dwdggbmjdDataEntity) {
        return dwdggbmjdDataMapper.insert(dwdggbmjdDataEntity);

    }
}
